package com.example.gw_back_end.config;

import com.example.gw_back_end.interceptors.ClientTypeInterceptor;
import com.example.gw_back_end.interceptors.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final LoginInterceptor loginInterceptor;
    private final ClientTypeInterceptor clientTypeInterceptor;
    public WebConfig(LoginInterceptor loginInterceptor, ClientTypeInterceptor clientTypeInterceptor) {
        this.loginInterceptor = loginInterceptor;
        this.clientTypeInterceptor = clientTypeInterceptor;
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login","/user/register","/test"/*,"/upload"*/); //定义无需拦截的接口
        registry.addInterceptor(clientTypeInterceptor)
                .addPathPatterns("/**") // 只拦截 /api/ 相关的请求
                .excludePathPatterns("/user/login","/user/register","/test"/*,"/upload"*/); //定义无需拦截的接口
    }

    // 后端解决跨域配置
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("http://localhost:8080")
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                .allowedHeaders("*")
//                .allowCredentials(true);
//    }
}
